package com.uni.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.uni.common.entity.TakerApply;
import com.uni.common.entity.User;
import com.uni.common.util.PageBean;
import com.uni.common.util.ReturnData;
import com.uni.dto.AddManageSchoolDTO;
import com.uni.dto.UserBaseInfoDTO;
import com.uni.dto.UserDTO;
import com.uni.dto.UserRoleDTO;
import com.uni.jjwt.JwtUtil;
import com.uni.mapper.UserMapper;
import com.uni.support.ThreadLocalUser;
import com.uni.web.util.WechatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
*	@author yanzheng_Yu
*	@date 2019年1月20日
*/
@Service
public class UserService{
	
	@Autowired
	private UserMapper userMapper;


	public User findById(Long id) {
		return userMapper.findById(id);
	}

	public void testLocal() {
		User user = ThreadLocalUser.getInstance().getUser();
		System.out.println(user.getId());
	}

	public List<UserDTO> findList(PageBean<UserDTO> pageBean) {
		return userMapper.findList(pageBean);
	}

	public Integer countList(PageBean<UserDTO> pageBean) {
		return userMapper.countList(pageBean);
	}


	@Transactional(rollbackFor=Exception.class)
	public int update(User user) {
		return userMapper.update(user);
	}

	public ReturnData bindPhone(String code, String encryptedData, String iv) {
		ReturnData rd = new ReturnData();
		String result = WechatUtil.getSessionKeyOropenid(code);
		if(StringUtils.isBlank(result)) {
			return rd.failBack("系统异常，请重试");
		}
		JSONObject wx = JSON.parseObject(result);
		String sessionKey = wx.getString("session_key");
		
		User user = ThreadLocalUser.getInstance().getUser();
		
		JSONObject userJson = WechatUtil.getUserInfo(encryptedData, sessionKey, iv);
		String phone = userJson.getString("purePhoneNumber");
		
		User updateUser = new User();
		updateUser.setId(user.getId());
		updateUser.setPhone(phone);
		updateUser.setCountryCode(userJson.getString("countryCode"));
		updateUser.setUpdateTime(new Date());
		int count = userMapper.update(updateUser);
		updateUser.setId(null);
		if(count > 0) {
			return rd.okBack(updateUser);
		}
		return rd.failBack("失败");
	}


	@Transactional(rollbackFor=Exception.class)
	public int updateBalance(Map<String, Object> updateUser) {
		int oc = userMapper.updateBalance(updateUser);
		if(oc != 1) {
			//事务回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return 0;
		}
		return oc;
	}

	@Transactional(rollbackFor=Exception.class)
	public void updateDeductionNumber(Long userId, int deductionNumber) {
		userMapper.updateDeductionNumber(userId,deductionNumber);
	}

	public UserBaseInfoDTO findByBaseInfo(Long userId) {
		return userMapper.findByBaseInfo(userId);
	}

	public User findByBgLoginName(String bgLoginName) {
		return userMapper.findByBgLoginName(bgLoginName);
	}

	
	public int insert(User user) {
		return userMapper.insert(user);
	}

	public int updateUserStatus(String[] ids) {
		return userMapper.updateUserStatus(ids);
	}


//	@Transactional
//	public ReturnData updateUserRole(UserRoleDTO userRoleDTO){
//
//		int rs=userMapper.updateRole(userRoleDTO);
//		if (rs>0||userRoleDTO.getRole()==2){
//			manageSchoolMapper.deleteManageSchoolByUserId(userRoleDTO.getUserId());
//			String[] schoolIdList=userRoleDTO.getSchoolIdList().split(",");
//
//			if (StringUtils.isNotBlank(userRoleDTO.getSchoolIdList())){
//				AddManageSchoolDTO addManageSchoolDTO=new AddManageSchoolDTO();
//				addManageSchoolDTO.setSchoolIdList(schoolIdList);
//				addManageSchoolDTO.setUserId(userRoleDTO.getUserId());
//				int rs1=manageSchoolMapper.insertManageSchool(addManageSchoolDTO);
//				if (rs1==schoolIdList.length){
//					return new ReturnData().okBack("修改成功");
//				}
//			}else{
//				return new ReturnData().okBack("修改成功");
//			}
//
//			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//			return new ReturnData().failBack("修改失败");
//		}
//		return new ReturnData().failBack("当前没有修改任何信息");
//	}
}
